<?php
namespace Admin\Controller;
use Think\Controller;

//加载GatewayClient
require_once SITE_PATH.'/vendor/workerman/GatewayClient/Gateway.php';
use GatewayClient\Gateway;
class PointsController extends BaseController{
	protected function message_record($user_id,$message,$send_user_id = 1){
		$data['create_time'] = time();
		$data['user_id'] = $user_id;
		$data['message'] = $message;
		$data['send_user_id'] = $send_user_id;
		$messageRecord = M('message');
		$message_res = $messageRecord ->add($data);
		if($message_res){
			return true;
		}else{
			return false;
		}
	}
	//资金记录
	protected function money_record($user_id,$type,$amount,$code,$status){
		$data['create_time'] = time();
		$data['user_id'] = $user_id;
		$data['type'] = $type;
		$data['amount'] = $amount;
		$data['code'] = $code;
		$data['status'] = $status;
		$MoneyRecord = M('money_record');
		$money_record_res = $MoneyRecord ->add($data);
		if($money_record_res){
			return true;
		}else{
			return false;
		}
	}

	protected function send_message_touid($uid,$message){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToUid($uid, $message);
	}
	
	protected function send_message_toall($message,$client_id_array=null ,$exclude_client_id){
		// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
		Gateway::$registerAddress = '127.0.0.1:1236';
		// 向任意群组的网站页面发送数据
		Gateway::sendToAll($message,$client_id_array=null ,Gateway::getClientIdByUid($exclude_client_id));
	}
	
	public function add(){
		$quest = M('recharge');
		$count = $quest->where("status = 0")->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $quest->where("status = 0")->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
		for($i=0;$i<count($list);$i++){
			$user = M('user')->where("id = {$list[$i]['user_id']}")->find();
			$list[$i]['nickname'] = $user['nickname'];
			$list[$i]['realname'] = $user['realname'];
			$list[$i]['alipay'] = $user['alipay_id'];
			$list[$i]['wechat'] = $user['wechat_id'];
			$bank = M('bank')->where("user_id = {$list[$i]['user_id']}")->find();
			$list[$i]['bank'] = $bank;
		}
		
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function del(){
		$quest = M('withdraw');
		$count = $quest->where("status = 0")->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $quest->where("status = 0")->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
		for($i=0;$i<count($list);$i++){
			$user = M('user')->where("id = {$list[$i]['user_id']}")->find();
			$list[$i]['nickname'] = $user['nickname'];
			$list[$i]['realname'] = $user['realname'];
			$list[$i]['alipay'] = $user['alipay_id'];
			$list[$i]['wechat'] = $user['wechat_id'];
			$bank = M('bank')->where("user_id = {$list[$i]['user_id']}")->find();
			$list[$i]['bank'] = $bank;
			$list[$i]['RMBamount'] = $list[$i]['amount'] * C('minertoRMB');
		}
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function agree_add(){
		$id = I('id');
		$info = M('recharge')->where("id = $id")->find();
		M('user')->where("id = {$info['user_id']}")->setInc('money',$info['amount']);
		$res = M('recharge')->where("id = $id")->setField("status",1);
		if($res){
			$message_points = array(
				'from'=>'admin',
				'type' => 'points',
				'content'=>	'您的充值请求已经通过，请查看余额情况！',
				'time'=>date('H:i:s')
			);
			$this->send_message_touid($info['user_id'],json_encode($message_points));
			$this->message_record($info['user_id'],'您的充值请求已经通过，请查看余额情况！');
			$type = 2;
			$code = 5;
			$status = 1;
			$this->money_record($info['user_id'],$type,$info['amount'],$code,$status);
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}
	
	
	public function agree_del(){
		$id = I('id');
		$info = M('withdraw')->where("id = $id")->find();
		$user = M('user')->where("id = {$info['user_id']}")->find();
		if($user['balance']<$info['amount']){
			$this->error('用户矿石不足');
		}
		M('user')->where("id = {$info['user_id']}")->setDec('balance',$info['amount']);
		M('user')->where("id = {$info['user_id']}")->setDec('freeze',$info['amount']);
		$res = M('withdraw')->where("id = $id")->setField("status",1);
		
		if($res){
			$message_points = array(
				'from'=>'admin',
				'type' => 'points',
				'content'=>	'您的提现请求已经通过，请查看提现账户余额情况！',
				'time'=>date('H:i:s')
			);
			$this->send_message_touid($info['user_id'],json_encode($message_points));
			$this->message_record($info['user_id'],'您的提现请求已经通过，请查看提现账户余额情况！');
			$type = 1;
			$code = 6;
			$status = 1;
			$this->money_record($info['user_id'],$type,$info['amount'],$code,$status);
			$this->success('成功');
		}else{
			$this->error('失败');	
		}
	}
	
	public function del_del_quest(){
		$id = I('id');
		$res = M('withdraw')->where("id = $id")->delete();
		if($res){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败');
		}
	}
	public function del_quest(){
		$id = I('id');
		$res = M('recharge')->where("id = $id")->delete();
		if($res){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败');
		}
	}
	public function dis_del_agree(){
		$id = I('id');

		$res = M('withdraw')->where("id = $id")->setField('status',2);
		$info = M('withdraw')->where("id = $id")->find();
		M('user')->where("id = {$info['user_id']}")->setInc('balance',$info['amount']);
		M('user')->where("id = {$info['user_id']}")->setDec('freeze',$info['amount']);
		if($res){
			$message_points = array(
				'from'=>'admin',
				'type' => 'points',
				'content'=>	'您的提现申请已经被拒绝，如有疑问请联系客服！',
				'time'=>date('H:i:s')
			);
			$this->send_message_touid($info['user_id'],json_encode($message_points));
			$this->message_record($info['user_id'],'您的提现申请已经被拒绝，如有疑问请联系客服！');
			$this->success('拒绝成功！');
		}else{
			$this->error('拒绝失败');
		}
	}	
	public function dis_agree(){
		$id = I('id');
		$res = M('recharge')->where("id = $id")->setField('status',2);
		$info = M('recharge')->where("id = $id")->find();
		if($res){
			$message_points = array(
				'from'=>'admin',
				'type' => 'points',
				'content'=>	'您的充值请求已经被拒绝，如有疑问请联系客服！',
				'time'=>date('H:i:s')
			);
			$this->send_message_touid($info['user_id'],json_encode($message_points));
			$this->message_record($info['user_id'],'您的充值请求已经被拒绝，如有疑问请联系客服！');
			$this->success('拒绝成功！');
		}else{
			$this->error('拒绝失败');
		}
	}
	
}



?>